package com.web.controller.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.dao.MskBuyOrderMapper;
import com.web.domain.WebResponse;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.domain.dataTable.BootstrapTableResponse;
import com.web.entity.MskBuyOrder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author FDjavaone
 * @date 2017/9/29
 */

@Slf4j
@Controller
public class MskBuyOrderController {
    @Autowired
    private MskBuyOrderMapper mskBuyOrderMapper;

    @RequestMapping(value = "sysb/buy",
            method = RequestMethod.GET)
    public String config(Model model, HttpServletRequest request) {
        return "MskBuyOrder";
    }


    @Data
    public static class MskBuyColumns extends BootstrapTableColumns {
        private String name;
        private String phone;
        private String status;
        private String isFirst;
    }


    @RequestMapping("/sysb/getMskBuy")
    @ResponseBody
    public BootstrapTableResponse getMskBuy(MskBuyColumns mskBuyColumns) {
        PageHelper.startPage(mskBuyColumns.getPageNumber(), mskBuyColumns.getPageSize());
        Page<MskBuyOrder> page = mskBuyOrderMapper.selectAllMskBuyOrder(mskBuyColumns);
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }


    @RequestMapping("/sysb/setMskBuyOrderFirst")
    @ResponseBody
    public WebResponse setMskBuyOrderFirst(MskBuyOrder mskBuyOrder) {
        Date date = new Date();
        mskBuyOrderMapper.resetIsFirstMskBuyOrder();
        MskBuyOrder mskBuyOrder1 = mskBuyOrderMapper.selectByPrimaryKey(mskBuyOrder.getId());
        if (mskBuyOrder1.getStatus() == 2) {
            return WebResponse.fail("操作失败，该条挂买单已经成交！");
        } else {
            mskBuyOrder.setIsFirst(1);
            mskBuyOrder.setModifyTime(date);
            mskBuyOrderMapper.updateByPrimaryKeySelective(mskBuyOrder);
            return WebResponse.ok("操作成功！");
        }


    }

}
